/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class Constructor implements IElement {

    public enum Kind { M, L, V, R, K, U, G,}

    public static final String NAME = "Constructor";

    private INode parentNode;
    private Kind kind = null;
    private Integer nr = null;
    private String aid = null;
    private Integer relnr = null;
    private Integer redefnr = null;
    private Integer superfluous = null;
    private Integer absredefnr = null;
    private String redefaid = null;
    private Integer structmodeaggrnr = null;
    private Integer aggregbase = null;

    private Properties properties = null;
    private ArgTypes argTypes = null;
    private StructLoci structLoci = null;
    private LinkedList<Typ> typList = new LinkedList<Typ>();
    private Fields fields = null;


    private Constructor(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static Constructor newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        Constructor constructor = new Constructor(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
            if (xmlAttribute.getName().equals("kind")){
                if (constructor.kind == null){
                    if (Kind.valueOf(xmlAttribute.getValue()) != null) {
                        constructor.kind = Kind.valueOf(xmlAttribute.getValue());
                    } else 
                        throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("nr")){
                if (constructor.nr == null){
                    try {constructor.nr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("aid")){
                if (constructor.aid == null){
                    constructor.aid = xmlAttribute.getValue();
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("relnr")){
                if (constructor.relnr == null){
                    try {constructor.relnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("redefnr")){
                if (constructor.redefnr == null){
                    try {constructor.redefnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("superfluous")){
                if (constructor.superfluous == null){
                    try {constructor.superfluous = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("absredefnr")){
                if (constructor.absredefnr == null){
                    try {constructor.absredefnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("redefaid")){
                if (constructor.redefaid == null){
                    constructor.redefaid = xmlAttribute.getValue();
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("structmodeaggrnr")){
                if (constructor.structmodeaggrnr == null){
                    try {constructor.structmodeaggrnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("aggregbase")){
                if (constructor.aggregbase == null){
                    try {constructor.aggregbase = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
                throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (xmlElement.getName().equals("Properties")){
                    if (constructor.properties == null) {
                        constructor.properties = Properties.newInstance(constructor, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Properties> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("ArgTypes")){
                    if (constructor.argTypes == null) {
                        constructor.argTypes = ArgTypes.newInstance(constructor, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <ArgTypes> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("StructLoci")){
                    if (constructor.structLoci == null) {
                        constructor.structLoci = StructLoci.newInstance(constructor, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <StructLoci> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Typ")){
                    constructor.typList.add(Typ.newInstance(constructor, xmlElementList));
                } else if (xmlElement.getName().equals("Fields")){
                    if (constructor.fields == null) {
                        constructor.fields = Fields.newInstance(constructor, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Fields> in element " + NAME, xmlElementList);
                    }
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return constructor;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.kind != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("kind", this.kind.name());
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.nr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("nr", this.nr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.aid != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("aid", this.aid);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.relnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("relnr", this.relnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.redefnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("redefnr", this.redefnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.superfluous != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("superfluous", this.superfluous);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.absredefnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("absredefnr", this.absredefnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.redefaid != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("redefaid", this.redefaid);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.structmodeaggrnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("structmodeaggrnr", this.structmodeaggrnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.aggregbase != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("aggregbase", this.aggregbase);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = !(properties != null
         || argTypes != null || structLoci != null
         || !typList.isEmpty() || fields != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.properties != null){
            xmlElementList.addAll(this.properties.getXMLElementList());
        }
        if (this.argTypes != null){
            xmlElementList.addAll(this.argTypes.getXMLElementList());
        }
        if (this.structLoci != null){
            xmlElementList.addAll(this.structLoci.getXMLElementList());
        }
        for (Typ typ : this.typList){
            xmlElementList.addAll(typ.getXMLElementList());
        }
        if (this.fields != null){
            xmlElementList.addAll(this.fields.getXMLElementList());
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Kind getKind(){
        return kind;
    }

    public Integer getNr(){
        return nr;
    }

    public String getAid(){
        return aid;
    }

    public Integer getRelnr(){
        return relnr;
    }

    public Integer getRedefnr(){
        return redefnr;
    }

    public Integer getSuperfluous(){
        return superfluous;
    }

    public Integer getAbsredefnr(){
        return absredefnr;
    }

    public String getRedefaid(){
        return redefaid;
    }

    public Integer getStructmodeaggrnr(){
        return structmodeaggrnr;
    }

    public Integer getAggregbase(){
        return aggregbase;
    }


    public Properties getProperties(){
        return properties;
    }

    public ArgTypes getArgTypes(){
        return argTypes;
    }

    public StructLoci getStructLoci(){
        return structLoci;
    }

    public LinkedList<Typ> getTypList(){
        return typList;
    }

    public Fields getFields(){
        return fields;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(Typ typ: this.typList) {
				typ.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
